<template>
    <div class="list">
        <div class="table" v-if="active==1">
            <div class="header" style="background-color:white;">
                <el-page-header @back="goBack" content="咨询与代理" style="position: relative;top: 10px;left: 5px;">
                </el-page-header>
            </div>
            <el-divider></el-divider>

            <el-form :model="params">
                <el-container>
                    <el-form-item>
                        <el-date-picker
                                v-model="params.collectionTime"
                                type="date"
                                placeholder="服务时间"
                                format="yyyy-MM-dd"
                                value-format="yyyy-MM-dd">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item style="margin-left: 10px">
                        <el-button type="primary" @click="handleSearch">检索</el-button>
                    </el-form-item>
                    <el-form-item style="margin-left: 10px">
                        <el-button @click="handleAdd">添加</el-button>
                    </el-form-item>
                </el-container>
            </el-form>

            <el-table :border="true" :data="pageResult.records">
                <el-table-column prop="id" v-if="false"></el-table-column>
                <el-table-column label="案号" prop="caseNo" width="150px"></el-table-column>
                <el-table-column label="委托人" prop="caseWtr"></el-table-column>
                <el-table-column label="服务时间" prop="collectionTime" width="100px"></el-table-column>
                <el-table-column label="服务类型" prop="caseTypeName" width="200px"></el-table-column>
                <el-table-column label="服务内容" prop="caseCause"></el-table-column>
                <el-table-column label="服务人次" prop="standard" width="100px"></el-table-column>
                <el-table-column label="服务费用" prop="caseAgencyfee" width="100px"></el-table-column>
                <el-table-column label="登记时间" prop="caseSubtime"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-dropdown trigger="click" @command="handleCommandClick">
                            <el-button>
                                操作<i class="el-icon-arrow-down el-icon--right"></i>
                            </el-button>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item :command="beforeHandlerCommand(scope.row,'show')">查看</el-dropdown-item>
                                <el-dropdown-item :command="beforeHandlerCommand(scope.row,'update')">修改</el-dropdown-item>
                                <el-dropdown-item :command="beforeHandlerCommand(scope.row,'create')">批量生产文书</el-dropdown-item>
                                <el-dropdown-item :command="beforeHandlerCommand(scope.row,'invoice')">申请发票</el-dropdown-item>
                                <el-dropdown-item :command="beforeHandlerCommand(scope.row,'record')">
                                    {{scope.row.caseRecord == 1 ?'档案归档':'取消档案归档'}}
                                </el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
            <el-footer class="case-list-footer">
                <el-pagination
                        background
                        layout="prev, pager, next"
                        :total="pageResult.total"
                        :page-size="pageResult.size"
                        class="pagination"
                        @current-change="handleIndexChange"
                >
                </el-pagination>
            </el-footer>
        </div>

        <!--添加修改案件-->
        <div class="box" v-if="active==2">
            <zxdl-add :law="law" :status="status" :cwPerformancesplitList="cwPerformancesplitList" @add="handleAddSubmit" @update="handleUpdateSubmit" @back="back"></zxdl-add>
        </div>
        <!--费用减免申请-->
        <div class="box" v-if="active==3">
            <feiyong-add :law="law" :active="active" @completed="handleCompleted" @back="back"></feiyong-add>
        </div>

        <!--查看详情-->
        <div class="box" v-if="active==4">
            <zxdl-details :law="law" :cwPerformancesplitList="cwPerformancesplitList" @back="back"></zxdl-details>
        </div>

        <!--批量生产文书-->
        <div class="box" v-if="active==5">
            <auto :law="law" @back="back" @completed="handleCompleted"></auto>
        </div>

        <!--申请发票-->
        <div class="box" v-if="active==6">
            <law-invoice :law="law" @back="back" @completed="handleCompleted"></law-invoice>
        </div>
    </div>
</template>

<script>
    //导入组件
    const ZxdlAdd = () => import('./ZxdlAdd.vue')
    const FeiyongAdd = () => import('../Feiyong/FeiyongAdd.vue')
    const ZxdlDetails = () => import('./ZxdlDetails.vue')
    const Auto = () => import('../Yongyin/Auto.vue')
    const LawInvoice = () => import('../CaseList/LawInvoice.vue')
    //导入工具类
    import {DateUtil} from "../../../model/common/DateUtil";
    import CaseLawService from '../../../model/gs/CaseLawService.js'
    import {CwPerformancesplitService} from "../../../model/ww/cw_performancesplit/CwPerformancesplitService";

    const caseLawService = CaseLawService.getInstance()
    const cwPerformancesplitService = CwPerformancesplitService.getInstance()

    export default {
        data() {
            return {
                active: 1,
                status:'add',
                params:{
                    caseType:1011,
                    collectionTime:'',
                    tab:'zxdl',
                    isOwn:'1',
                    recycle:'N'
                },
                pageResult:[],
                law:{
                    collectionTime:DateUtil.today(),
                    caseLawsuitobj:1000,
                    caseCaseTypeId:'101101'
                },
                cwPerformancesplitList:[
                    {
                        hrEmpId:'',
                        caseAttorney:'主办律师',
                        username:'',
                        cwPerformancesplit:'',
                        cwSplitType:'百分比',
                        cwRealSplitMoney:'',
                        type:2
                    }
                ]
            }
        },
        methods:{
            //跳转添加页面的方法
            handleAdd(){
                this.status = 'add'
                this.active++
            },
            //跳转修改的方法
            handleUpdate(law){
                //查询案件信息和办案律师信息
                this.$axios.all([caseLawService.getById(law.id),
                    cwPerformancesplitService.getByCaseLaw(law.id)])
                    .then(this.$axios.spread((response1,response2) => {
                        this.law = response1.data.data
                        this.cwPerformancesplitList = response2.data.data
                        this.active++
                        this.status = 'update'
                    }))
            },
            //添加
            handleAddSubmit(){
                //校验
                if(!caseLawService.zxdlCheck(this,this.law,this.cwPerformancesplitList)){
                    return
                }
                //查询出最大编号并赋值
                caseLawService.getCode().then(response => {
                    if(response.data.code === 0){
                        let num = response.data.data
                        if(num < 10){
                            num = '0'+num
                        }
                        this.law.caseNo =
                            new Date().getFullYear()+'(文)第00'
                            +num+'号'
                        //调用方法保存案件
                        caseLawService.save(this.law,this.cwPerformancesplitList).then(response => {
                            if(response.data.code === 0){
                                const flag = this.law.caseAgencyfee < this.law.caseLawsuitobj
                                this.law = response.data.data
                                this.replacecwPerformancesplitList()
                                //判断代理费是否小于标的
                                if(flag){
                                    this.$message.warning({
                                        message:'代理费低于最低标的，将申请费用减免',
                                        duration:1000
                                    })
                                    //切换到费用减免申请页面
                                    this.active = 3
                                }else{
                                    //重置数据
                                    this.handleCompleted()
                                    this.$message.success({
                                        message:'添加成功',
                                        duration:1000
                                    })
                                }
                            }
                        })
                    }
                })
            },
            handleUpdateSubmit(){
                //校验
                if(!caseLawService.zxdlCheck(this,this.law,this.cwPerformancesplitList)){
                    return
                }
                //调用方法保存案件
                caseLawService.updateWithlawyer(this.law,this.cwPerformancesplitList).then(response => {
                    if(response.data.code === 0){
                        this.replacecwPerformancesplitList()
                        //重置数据
                        this.handleCompleted()
                        this.$message.success({
                            message:'修改成功',
                            duration:1000
                        })
                    }
                })
            },
            //拿到案件的方法
            getCaseLawList(){
                caseLawService.getByPage(this.params).then(response => {
                    if(response.data.code === 0){
                        this.pageResult = response.data.data
                    }
                })
            },
            //处理角标变化的方法
            handleCommandClick(command){
                switch (command.type) {
                    case 'show': this.showDetails(command.law);break;
                    case 'create': this.handleCreate(command.law);break;
                    case 'invoice': this.handleInvoice(command.law);break;
                    case 'update': this.handleUpdate(command.law);break;
                    case 'record': this.handleRecord(command.law);break;
                }
            },
            //处理分享/合同/档案点击的方法
            handleRecord(law){
                //判断当前是哪个状态
                if(law.caseRecord == '1'){
                    law.caseRecord = '2'
                }else{
                    law.caseRecord = '1'
                }
                //调用方法更新
                caseLawService.update(law).then(response => {
                    if(response.data.code === 0){
                        this.getCaseLawList()
                    }
                })
            },
            //查看详情的方法
            showDetails(law){
                //查询案件信息和办案律师信息
                this.$axios.all([caseLawService.getById(law.id),
                    cwPerformancesplitService.getByCaseLaw(law.id)])
                    .then(this.$axios.spread((response1,response2) => {
                        this.law = response1.data.data
                        this.cwPerformancesplitList = response2.data.data
                        this.active = 4
                    }))
            },
            //批量生产文书的方法
            handleCreate(law){
                caseLawService.getById(law.id).then(response => {
                    this.law = response.data.data
                    this.active = 5
                })
            },
            //申请发票的方法
            handleInvoice(law){
                caseLawService.getById(law.id).then(response => {
                    this.law = response.data.data
                    this.active = 6
                })
            },
            //角标触发之前的事件
            beforeHandlerCommand(law,type){
                return {
                    law,type
                }
            },
            //检索按钮点击事件
            handleSearch(){
                //重置当前页为1
                this.params.page = 1
                this.getCaseLawList()
            },
            //当前页放生变化的事件
            handleIndexChange(index){
                this.params.page = index
                this.getCaseLawList()
            },
            goBack(){
                history.back()
            },
            back(){
                this.replaceLaw()
                this.replacecwPerformancesplitList()
                this.active = 1
            },
            handleCompleted(){
                //刷新数据
                this.getCaseLawList()
                this.active = 1
                //重置law数据
                this.replaceLaw()
            },
            //重置数据的方法
            replacecwPerformancesplitList(){
                this.cwPerformancesplitList = [
                    {
                        hrEmpId:'',
                        caseAttorney:'主办律师',
                        username:'',
                        cwPerformancesplit:'',
                        cwSplitType:'百分比',
                        cwRealSplitMoney:'',
                        type:2
                    }
                ]
            },
            //重置案件信息的方法
            replaceLaw(){
                this.law = {
                    collectionTime:DateUtil.today(),
                    caseLawsuitobj:1000,
                    caseCaseTypeId:'101101'
                }
            }
        },
        created(){
            this.getCaseLawList()
        },
        components:{
            ZxdlAdd,
            FeiyongAdd,
            ZxdlDetails,
            Auto,
            LawInvoice
        }
    }
</script>

<style scoped>
    .list{
        width: 100%;
    }
    .table{
        width: 100%;
    }
    .case-list-footer{
        margin-top: 20px;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
    }
    .box{
        width: 100%;
    }
</style>
